const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin });
let input = [];

rl.on('line', line => input.push(line.trim()));
rl.on('close', () => {
  let idx = 0;
  const T = parseInt(input[idx++]);

  for (let t = 0; t < T; t++) {
    const n = parseInt(input[idx++]);
    const a = [0, ...input[idx++].split(' ').map(Number)]; // 1-indexed
    
    let totalContribution = 0;

    // 枚举所有区间 [l, r]，其中 1 <= l < r <= n
    for (let l = 1; l < n; l++) {
      for (let r = l + 1; r <= n; r++) {
        // 计算区间 [l, r] 的贡献
        let contribution = 0;
        
        // 枚举区间内部的所有位置 i（l < i < r）
        for (let i = l + 1; i < r; i++) {
          // 检查 ai < al 或 ai < ar
          if (a[i] < a[l] || a[i] < a[r]) {
            contribution++;
          }
        }
        
        totalContribution += contribution;
      }
    }

    console.log(totalContribution);
  }
});

